<template>
  <el-dialog :title="title" :visible.sync="open" :width="width" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item
        v-for="item in dialogFormList"
        :key="item.prop"
        :label="item.label"
        :prop="item.prop"
      >
        <el-input
          v-if="item.type === 'input'"
          v-model="form[item.prop]"
          :maxlength="item.maxlength ? item.maxlength : ''"
          placeholder="请输入"
        />
        <el-input
          type="textarea"
          v-if="item.type === 'textarea'"
          v-model="form[item.prop]"
          :maxlength="item.maxlength ? item.maxlength : ''"
          placeholder="请输入"
        ></el-input>
        <el-radio-group v-if="item.type === 'radio'" v-model="form[item.prop]">
          <el-radio
            v-for="dict in item.dict"
            :key="dict.value"
            :label="dict.value"
            >{{ dict.label }}</el-radio
          >
        </el-radio-group>
        <el-date-picker
          v-if="item.type === 'daterange'"
          v-model="form[item.prop]"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
        <el-select v-model="form[item.prop]" placeholder="请选择" clearable>
          <el-option
            v-for="dict in item.dict"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        <treeselect
          v-if="item.type === 'treeSelect'"
          v-model="form[item.prop]"
          :options="item.list"
          :show-count="true"
          placeholder="请选择"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button
        v-if="type !== 'view'"
        type="primary"
        size="mini"
        class="tableAdd"
        @click="submitForm"
        >确 定</el-button
      >
      <el-button class="dialogCancel" type="primary" size="mini" @click="cancel"
        >取 消</el-button
      >
      <slot></slot>
    </div>
  </el-dialog>
</template>
<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "DialogPanel",
  components: {
    Treeselect,
  },
  props: {
    title: {
      type: String,
      default: "",
    },
    width: {
      type: String,
      default: "500px",
    },
    open: {
      type: Boolean,
      default: false,
    },
    form: {
      type: Object,
      default: () => {},
    },
    rules: {
      type: Object,
      default: () => {},
    },
    dialogFormList: {
      type: Array,
      default: () => [],
    },
    type: {
      type: String,
      default: "",
    },
  },
  watch: {
    open: {
      handler(val) {
        this.visibleOpen = val;
      },
    },
  },
  data() {
    return {
      visibleOpen: false,
    };
  },
  methods: {
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.$emit("submitForm");
        }
      });
    },
    cancel() {
      this.visibleOpen = false;
      this.$emit("cancel");
    },
  },
};
</script>
